package  
{
	import caurina.transitions.*;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	/**
	 * ...
	 * @author René Verheij
	 */
	public class Pulse extends Sprite
	{
		private var _color:Object;
		private var _pawn:Pawn;
		public function Pulse(color:Object,pPawn:Pawn) 
		{
			_pawn = pPawn;
			_color = color;
			
			graphics.lineStyle(color.lineWidth, color.color, color.alpha);
			if (Main.instance.colors.getKey(color) == 'BLUE')
			{
				var otherGreens:Array = [];
				for each(var pawn:Pawn in Main.instance.pawns)
				{
					if (pawn.color === _color && pawn !== _pawn)
					{
						otherGreens.push(pawn);
					}
				}
				if (otherGreens.length > 0)
				{
					var target:Pawn = otherGreens[Math.floor(Math.random() * otherGreens.length)];
					graphics.lineTo(-(_pawn.x-target.x)/_color.scale, -(_pawn.y-target.y)/_color.scale);
				//graphics.drawRect(-1,-1,2,2);
				//graphics.drawCircle(0, 0, 1);
				}
			}
			else
			{
				graphics.drawCircle(0, 0, 1);
			}
			addEventListener(Event.ADDED_TO_STAGE, startPulse);
		}
		
		private function startPulse(e:Event):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, startPulse);
			
			//Tweener.addTween(this, { scaleX:_color.scale, scaleY:_color.scale, time:_color.time, alpha:0, transition:_color.transition,onComplete:waitTillRemove } );			
			Tweener.addTween(this, { scaleX:_color.scale, scaleY:_color.scale, time:_color.transitionTime, alpha:_color.endAlpha, transition:_color.transition,onComplete:waitTillRemove } );			
		}
			
		private function waitTillRemove():void
		{
			Tweener.addTween(this, { alpha:0, time:_color.remain,onComplete:removeMe} );			
			//var timer:Timer = new Timer(_color.remain, 1);
			//timer.addEventListener(TimerEvent.TIMER_COMPLETE, removeMe);
		}
		private function removeMe():void 
		{
			Main.instance.removeChild(this);
			
		}
		
	}

}